<?php


namespace People\Controller;

use Think\Controller;
use Think\Model;


class IndexController extends Controller
{
    /**
     * 业务逻辑都放在 WeiboApi 中
     * @var
     */
    public function _initialize()
    {
        $tabList=S('help_type_list');
        if(!$tabList){
            $tabList=D('HelpType')->order('id asc')->select();
            S('help_type_list',$tabList);
        }
        $this->assign('tabList',$tabList);
        $this->setTitle('大神');
    }

    public function index($page = 1)
    {
        $aDoing=I('tabId',0,'intval');
        $aSort=I('sort','is_recommend','op_t');

        $this->assign('sort',$aSort);
        $this->assign('tabHash',$aDoing);

        $order=$aSort.' desc';

        //获取校园成员id
        $expectIds=D('AuthGroupAccess')->where(array('group_id'=>2))->select();//group_id=2是校园用户组id
        if(is_array($expectIds)&&count($expectIds)){
            $expectIds=array_column($expectIds,'uid');
            $map['uid']=array('not in',$expectIds);//排除校园成员
        }
        $map['is_production']=0;//排除官方制片成员
        $map['status']=1;
        $map['last_login_time']=array('neq',0);
        if($aDoing){
            $map['_string'] = ' doing1='.$aDoing.' or doing2='.$aDoing;
        }
        $model=D('Member');
        $totalCount=$model->where($map)->count();
        if($aSort=='fans'){
            $Model=new Model();
            if($aDoing){
                $doingSql = 'AND (  a.doing1='.$aDoing.' or a.doing2='.$aDoing.')';
            }
            if(is_array($expectIds)&&count($expectIds)){
                $expectIds=implode(',',$expectIds);
                $expectSql= 'AND a.uid NOT IN ('.$expectIds.') ';
            }
            $peoples =$Model->query('SELECT a.uid,count(b.follow_who) as fans FROM '.C('DB_PREFIX').'member as a left join '.C('DB_PREFIX').'follow as b on a.uid = b.follow_who WHERE a.status = 1 AND a.last_login_time <> 0 AND a.is_production = 0 '.$expectSql.$doingSql.' group by a.uid  order by '.$order.' limit '.(($page-1)*50).',50');
        }else{
            $peoples =$model->where($map)->order($order)->page($page,50)->field('uid')->select();
        }
        foreach ($peoples as &$v) {
            $v = query_user(array('uid','avatar128', 'space_url', 'fans', 'nickname','doing','address','help_count','sex','rank_link','sentiment','rate','user_help_3'), $v['uid']);


            $v['verify_info']=verify_info($v['uid']);

        }
        unset($v);
        $this->assign('totalCount',$totalCount);
        $this->assign('lists', $peoples);
        $this->setTitle('大神列表页面');
        $this->display();
    }

    public function find($page = 1, $keywords = '')
    {
        $nickname = op_t($keywords);
        if ($nickname != '') {
            $map['nickname'] = array('like','%'.$nickname.'%');
        }
        $list = D('Member')->where($map)->findPage(18);
        foreach ($list['data'] as &$v) {
            $v['user'] = query_user(array('avatar128', 'space_url', 'username', 'fans', 'following', 'signature', 'nickname'), $v['uid']);
        }
        unset($v);
        $this->assign('lists', $list);
        $this->assign('nickname',$nickname);
        $this->display();
    }
}